package com.glodon.paas.document.dao;

import java.util.Date;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glodon.paas.document.constant.DocumentConstant;
import com.glodon.paas.document.dao.domain.FileInfoEntity;
import com.glodon.paas.test.util.MultProcesser;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:test-document-dao.xml", "classpath:paas-document-dao-mysql.xml" })
@Ignore
public class FileInfoDaoPerformance {

    @Autowired()
    FileDao fileDao;

    String  DEFAULT_FILE_ID  = "DEFAULT_FILE_ID";
    String  DEFAULT_OWNER_ID = "DEFAULT_OWNER_ID";
    long    createTimeSecons = System.currentTimeMillis() / 1000;

    @Test
    public void testDefault() {
    }

    /*
     * 初始化数据如下： id, fil --------------------------------------- t01, d testFolder_01 t02, d testFolder_02 t03, d
     * testFolder_01 t04, - testFile_01.txt 100kb t05, - testFile_02.txt 200kb t06, d testFolder_02 t07, -
     * testFile_01.txt 100kb t08, - testFile_02.txt 200kb t09, - testFile_03.txt 300kb deleted t10, - testFile_01.txt
     * t11, - testFile_02.pdf t12, - testFile_03.pdf deleted t13, d testFolder_03 deleted t14, - testFile_01.txt 主动删除
     * t15, - testFile_02.txt 级联删除
     */
    public void initData() {
        // fileDao.insert(newFile(id, parentId, type, name, extension, size));
        fileDao.insert(newFile("t01", "", DocumentConstant.FILE_TYPE_FOLDER, "testFolder_01", null, 0L));
        fileDao.insert(newFile("t02", "", DocumentConstant.FILE_TYPE_FOLDER, "testFolder_02", null, 0L));
        fileDao.insert(newFile("t03", "t02", DocumentConstant.FILE_TYPE_FOLDER, "testFolder_01", null, 0L));
        fileDao.insert(newFile("t04", "t03", DocumentConstant.FILE_TYPE_FILE, "testFile_01", "txt", 100L));
        fileDao.insert(newFile("t05", "t03", DocumentConstant.FILE_TYPE_FILE, "testFile_02", "txt", 200L));

        fileDao.insert(newFile("t06", "t02", DocumentConstant.FILE_TYPE_FOLDER, "testFolder_02", null, 0L));
        fileDao.insert(newFile("t07", "t06", DocumentConstant.FILE_TYPE_FILE, "testFile_01", "txt", 100L));
        fileDao.insert(newFile("t08", "t06", DocumentConstant.FILE_TYPE_FILE, "testFile_02", "txt", 200L));
        fileDao.insert(newFile("t09", "t06", DocumentConstant.FILE_TYPE_FILE, "testFile_03", "txt", 300L));
        fileDao.updateStatus("t09", DocumentConstant.FILE_STATUS_RECYCLE);
        fileDao.insert(newFile("t10", "", DocumentConstant.FILE_TYPE_FILE, "testFile_01", "txt", 100L));
        fileDao.insert(newFile("t11", "", DocumentConstant.FILE_TYPE_FILE, "testFile_02", "pdf", 200L));
        fileDao.insert(newFile("t12", "", DocumentConstant.FILE_TYPE_FILE, "testFile_03", "pdf", 300L));
        fileDao.updateStatus("t12", DocumentConstant.FILE_STATUS_RECYCLE);
        fileDao.insert(newFile("t13", "", DocumentConstant.FILE_TYPE_FOLDER, "testFolder_03", null, 0L));
        fileDao.insert(newFile("t14", "t13", DocumentConstant.FILE_TYPE_FILE, "testFile_01", "txt", 100L));
        fileDao.insert(newFile("t15", "t13", DocumentConstant.FILE_TYPE_FILE, "testFile_01", "txt", 200L));
        fileDao.updateStatus("t13", DocumentConstant.FILE_STATUS_RECYCLE);
        fileDao.updateStatus("t14", DocumentConstant.FILE_STATUS_RECYCLE);
        fileDao.updateStatus("t15", DocumentConstant.FILE_STATUS_RECYCLE_CASCADE);
    }

    // @Test
    // public void performanceSelectByComplexPerformance(){
    // if(!DocumentBaseIT.testPerformance){
    // return;
    // }
    // clean();
    // initData();
    // //final AtomicInteger indexGenerator = new AtomicInteger();
    // final TPS tps = new TPS("selectByComplex()",1);
    // tps.start();
    //
    // Runnable task = new Runnable(){
    // public void run() {
    // //int id = indexGenerator.getAndIncrement();
    // List<FileInfoEntity> list = fileDao.selectByComplex(DEFAULT_OWNER_ID, "", null, FILE_STATUS_NORMAL, null, 1, 0,
    // 100);
    // tps.increment();
    // }
    // };
    // runTask("testPerformance3Select()", task, 50, 100000);
    // //clean();
    // }

    private void runTask(String caseName, Runnable task, int threadCount, int totalCount) {
        MultProcesser pro = new MultProcesser(threadCount);
        long start = System.currentTimeMillis();
        pro.submit(task, totalCount);
        pro.shutdown();
        long end = System.currentTimeMillis();

        System.out.println(caseName + totalCount + ",time=" + (end - start) + " average time=" + (end - start)
                           / totalCount);
    }

    private void clean() {
        fileDao.cleanByOwnerId(DEFAULT_OWNER_ID);
    }

    /**
     * @param id
     * @param parentId
     * @param type
     * @param name
     * @param extension
     * @param size
     * @param createTimeSecond 创建时间秒
     * @return
     */
    private FileInfoEntity newFile(String id, String parentId, Integer type, String name, String extension, Long size) {
        FileInfoEntity record = new FileInfoEntity();
        record.setOwnerId(DEFAULT_OWNER_ID);
        record.setId(id);
        record.setParentId(parentId);
        record.setType(type);
        record.setName(name);
        record.setExtension(extension);
        record.setSize(size);
        record.setCount(0);
        record.setCreateTime(new Date((createTimeSecons++) * 1000));
        return record;
    }
}
